const express = require('express');
const router = express.Router();

const User = require('../models/user');

// login
router.post('/', (req, res) => {
  const username = req.body.username;
  const password = req.body.password;
  User.checkPassword(username, password).then(result => {
    return { result, username };
  })
  .then(({ result, username }) => {
    console.log(result)
    if (!result) {
      res.send({ success: result });
      return Promise.reject();
    }
    return Promise.all([User.selectUserByLoginName(username), Promise.resolve(result)])
  })
    .then(([user, result]) => {
      res.send({ success: result, user });
    })
    .catch(e => { })
})

module.exports = router
